package com.chucang.shucang.usr.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chucang.shucang.common.base.page.PageData;
import com.chucang.shucang.common.base.page.PageQuery;
import com.chucang.shucang.common.base.service.impl.CrudServiceImpl;
import com.chucang.shucang.common.security.utils.SecurityUtil;
import com.chucang.shucang.usr.constant.UsrCommonConstant;
import com.chucang.shucang.usr.dao.UserBillRecordDao;
import com.chucang.shucang.usr.dto.UserBillRecordDTO;
import com.chucang.shucang.usr.entity.UserBillRecordEntity;
import com.chucang.shucang.usr.entity.UserBlindBoxHoldEntity;
import com.chucang.shucang.usr.service.UserBillRecordService;
import com.chucang.shucang.usr.vo.BillPageResVO;
import com.chucang.shucang.usr.vo.query.BillQuery;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;


/**
 * 用户账单
 *
 * @author flitsneak flitsneak@gmail.com
 * @since 1.0.0 2022-10-22
 */
@Service
public class UserBillRecordServiceImpl extends CrudServiceImpl<UserBillRecordDao, UserBillRecordEntity, UserBillRecordDTO> implements UserBillRecordService {

    @Override
    public PageData<BillPageResVO> billPage(BillQuery billQuery) {

        LambdaQueryWrapper<UserBillRecordEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserBillRecordEntity::getUserId, SecurityUtil.getUser().getId());

        IPage<UserBillRecordEntity> page = this.page(
                new PageQuery<UserBillRecordEntity>().getPage(billQuery),
                lambdaQueryWrapper
        );

        if (CollUtil.isEmpty(page.getRecords())) {
            return new PageData<>(Collections.emptyList(), 0);
        }
        List<BillPageResVO> billPageResVOS = BeanUtil.copyToList(page.getRecords(), BillPageResVO.class);

        return new PageData<>(billPageResVOS, page.getTotal());
    }
}